package com.smartrobot.smartrobot.adapter;


import android.content.Context;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.*;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.smartrobot.smartrobot.Common.Common;
import com.smartrobot.smartrobot.Common.CommonFunc;
import com.smartrobot.smartrobot.DB.NewFriendSqlHelper;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.activity.New_friend_activity;
import com.smartrobot.smartrobot.circle.NewFriend.NewFriendUtil;

import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.pojo.NewFriendPojo;

import com.smartrobot.smartrobot.util.ImageLoaderApi;
import com.smartrobot.smartrobot.util.User;


import java.util.List;

/**
 * Created by yeluojun on 2015/1/19.
 */
public class New_friend_adapter extends BaseAdapter implements AdapterView.OnItemClickListener {

    private Context context;
    private List<NewFriendPojo> list;
    private ImageLoader imageLoader;

    public New_friend_adapter(Context context,List<NewFriendPojo> list){
        this.context = context;
        this.list = list;
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        try{
            ViewHolder holder;
            if (view == null){
                view =  LayoutInflater.from(context).inflate(R.layout.new_friend_item, viewGroup, false);
                holder = new ViewHolder();
                holder.headImg = (ImageView) view.findViewById(R.id.new_friend_headImg);
                holder.nickName = (TextView)view.findViewById(R.id.new_friend_nickname);
                holder.msg = (TextView)view.findViewById(R.id.new_friend_msg);
                holder.new_friend_status_ly = (RelativeLayout)view.findViewById(R.id.new_friend_status_ly);
                holder.new_friend_accept_ly = (RelativeLayout)view.findViewById(R.id.new_friend_accept_ly);
                holder.status = (TextView)view.findViewById(R.id.new_friend_status);
                holder.accept = (Button)view.findViewById(R.id.new_friend_accept);
                holder.refuse = (Button)view.findViewById(R.id.new_friend_refuse);

                view.setTag(holder);

            }else{
                holder = (ViewHolder)view.getTag();
            }
            holder.status.setText(list.get(i).getStatus());


            if(list.get(i).getName()!=null&&!list.get(i).getName().equals("")){
                holder.nickName.setText(list.get(i).getName());
            }else{
                holder.nickName.setText(list.get(i).getUserAccount());
            }

            if(list.get(i).getMsg().equals("")){
                holder.msg.setText("对方请求添加好友");
            }

            if(list.get(i).getStatus().equals("未验证")){
                holder.new_friend_accept_ly.setVisibility(View.VISIBLE);
                holder.new_friend_status_ly.setVisibility(View.GONE);
            }
            if(!list.get(i).getStatus().equals("未验证")){
                holder.new_friend_accept_ly.setVisibility(View.GONE);
                holder.new_friend_status_ly.setVisibility(View.VISIBLE);
            }


            imageLoader.displayImage(Common.baseUrl+list.get(i).getHeadImg(),holder.headImg, ImageLoaderApi.getOptions());
            buttonClick(holder,list.get(i));

        }catch (Exception e){
            Log.e("=======new friend adapter error======",e.toString());
            LogToSd.log_init();
            LogToSd.error("new friend adapter error:");
            LogToSd.error(e.toString());

        }
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }


    class ViewHolder{
//        RelativeLayout item;
        RelativeLayout new_friend_status_ly;
        RelativeLayout new_friend_accept_ly;
        ImageView headImg;
        TextView nickName;
        TextView msg;
        TextView status;
        Button accept;
        Button refuse;
    }


    void buttonClick(ViewHolder holder,final NewFriendPojo pojo){

        //拒绝
        holder.refuse.setOnClickListener(new onClick(pojo)) ;


        //接受
        holder.accept.setOnClickListener(new onClick(pojo) );
    }

    class onClick implements View.OnClickListener{

        NewFriendPojo pojo;

        public onClick(NewFriendPojo pojo){
            this.pojo = pojo;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.new_friend_accept:
                    New_friend_activity.accept(pojo);
                    break;
                case R.id.new_friend_refuse:
                    refuse(pojo);
                    break;

            }

        }
    }

    public  void changeNotify(){
        String as =   this.list.toString();
        Log.e("======!!@##!#!@!@!@=====",as);
        Log.e("list的长度:",this.list.size()+"");

        this.notifyDataSetChanged();
    }



    public void refuse(NewFriendPojo pojo){
        Log.e("拒绝你拉！！！！！！！！！！！！！！！！！！","");
        if(User.getInstance()==null){
            User.getUserInfo(context);
        }
        NewFriendUtil newFriendUtil = new NewFriendUtil(context, CommonFunc.stringToMD5(User.getInstance().getId()));
        newFriendUtil.updateStatus(NewFriendSqlHelper.UN_PASS,pojo.getUserId());

        pojo.setStatus(NewFriendSqlHelper.UN_PASS);
        this.notifyDataSetChanged();
    }





}
